<?php
/**
 * Created by Veyron
 * User: Vy
 * motto:现在的拼搏 为了更好的明天
 * day:  2020/1/13
 * Time: 22:38
 */
namespace app\common\model\mysql;
use think\Model;

class User extends Model{
    /**
     * 自动写入时间
     * @var bool
     */
    public $autoWriteTimestamp = true;
    /**
     * 根据手机号获取用户信息
     * @param $username
     * @return array|bool|Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getUserByPhoneNumber($phoneNumber){
        if(empty($phoneNumber)){
            return false;
        }
        $where = [
            'phone_number' => $phoneNumber
        ];
        $result = $this->where($where)->find();
        return $result;
    }
    public function getUserByUsername($username){
        if(empty($username)){
            return false;
        }
        $where = [
            'username' => $username
        ];
        $result = $this->where($where)->find();
        return $result;
    }

    /**
     * 根据主键ID更新数据表中的数据
     * updateById
     * @param $id
     * @param $data
     * @return bool
     */
    public function updateById($id,$data){
        $id = intval($id);
        if(empty($id) || empty($data) || !is_array($data)){
            return false;
        }
        $where = [
            'id' => $id
        ];
        return $this->where($where)->save($data);
    }

    /**
     * 根据id获取用户信息
     * @param $userId
     */
    public function getUserById($userId){
        $userId = intval($userId);
        if(!$userId){
            return false;
        }
        return $this->find($userId);
    }
}
